package com.yuncheng.spcyApi.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;


/**
 * 对象赋值
 */
public class ObjectCopyUtils {
    private static final Logger log = LoggerFactory.getLogger(ObjectCopyUtils.class);

    /**
     * 对象相同属性copy
     *
     * @param obj
     * @param toResult
     * @return
     * @throws Exception
     *             转换报错
     */
    public static <T> T cloneObj(Object obj, Class<T> toResult,List<String> keys) {
        if (obj == null) {
            return null;
        }
        try {
            T t = toResult.newInstance();
            Field[] fields = toResult.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);//修改访问权限
                if (Modifier.isFinal(field.getModifiers())) {
                    continue;
                }
                // 判断是否为要复制的字段,等于赋值
                if (keys.contains(field.getName())) {
                    String firstLetter = field.getName().substring(0, 1).toUpperCase(); // 首字母大写
                    String getMethodName = "get" + firstLetter + field.getName().substring(1);
                    String setMethodName = "set" + firstLetter + field.getName().substring(1);
                    Method getMethod = obj.getClass().getMethod(getMethodName);   //从源对象获取get方法
                    Method setMethod = toResult.getMethod(setMethodName, new Class[]{field.getType()}); //从目标对象获取set方法

                    Object value = getMethod.invoke(obj); // get 获取的是源对象的值
                    setMethod.invoke(t, new Object[]{value}); // set 设置的是目标对象的值
                }

            }
            return t;
        } catch (Exception e) {
            log.error("对象赋值出错【Exception】- {}",e);
        }
        return null;
    }

    /**
     * 将对象转成Map对象-模板字段
     * @param object
     * @return
     */
    public static Map<String,String> objectToMap(Object object) {
        Map<String,String> dataMap = new HashMap<>();
        BeanMap beanMap = BeanMap.create(object);
        for (Object key : beanMap.keySet()) {
            String k = "${data."+key+"}";
//            System.out.println("1:"+k);
//            System.out.println(beanMap.get(key));
            if (beanMap.get(key) != null){
                dataMap.put(k,beanMap.get(key).toString());
            }else {
//                dataMap.put(k,"xxxxx");
                dataMap.put(k," ");
            }
        }
        return dataMap;
    }

    /**
     * 将对象转成Map对象-模板字段
     * @param object
     * @return
     */
    public static HashMap<String,Object> objectToMap2(Object object) {
        HashMap<String,Object> dataMap = new HashMap<>();
        BeanMap beanMap = BeanMap.create(object);
        for (Object key : beanMap.keySet()) {
            String k = ""+key+"";
            if (beanMap.get(key) != null){
                dataMap.put(k,beanMap.get(key).toString());
            }else {
                dataMap.put(k,"无");
            }
        }
        return dataMap;
    }

    /**
     * 将对象转成ObjectMap对象-模板字段
     * @param object
     * @return
     */
    public static Map<String,Object> objectToObjectMap(Object object) {
        Map<String,Object> dataMap = new HashMap<>();
        BeanMap beanMap = BeanMap.create(object);
        for (Object key : beanMap.keySet()) {
            String k = ""+key+"";
            if (beanMap.get(key) != null){
                dataMap.put(k,beanMap.get(key));
            }else {
                dataMap.put(k,"xxxxx");
            }
        }
        return dataMap;
    }

    /**
     * data关键字map转换 无data关键字map
     * @param parMap
     * @return
     */
    public static Map<String, Object> templateDataKeyMapToKey(Map<String, String> parMap){
        Map<String, Object> resultMap = new HashMap<>();
        String key = "";
        for (Map.Entry<String,String> entry : parMap.entrySet()){
            key = "";

            if (StringUtils.isNotBlank(entry.getKey()) && entry.getKey().contains("${data.")){
                key = entry.getKey().substring(7, entry.getKey().length()-1);
            }else {
                key = entry.getKey();
            }

            resultMap.put(key, entry.getValue());
        }

        return resultMap;
    }

    /**
     * 将Map转为StringMap-模板字段
     */
    public Map<String,String> MapObjectToMap(Map<String,Object> orgMap){
        Map<String,String> dataMap = new HashMap<>();
        for (Map.Entry<String, Object> e : orgMap.entrySet()) {
            if (e.getValue() != null) {
                dataMap.put(e.getKey(),e.getValue().toString());
            }else {
                dataMap.put(e.getKey(),"xxxxx");
            }
        }

        return dataMap;
    }
}
